package com.br.sae.conexaobd;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

import com.br.sae.model.entitys.Administrador;
import com.br.sae.model.entitys.Aluno;
import com.br.sae.model.entitys.Disciplina;
import com.br.sae.model.entitys.DisciplinaProfessor;
import com.br.sae.model.entitys.Matricula;
import com.br.sae.model.entitys.Noticia;
import com.br.sae.model.entitys.Professor;
import com.br.sae.model.entitys.Turma;
import com.br.sae.model.entitys.Usuario;

public class SistemaDAO<T> {
	protected static EntityManagerFactory entityFactory;
	protected static EntityManager entityManager;	
	static {
		entityFactory = Persistence.createEntityManagerFactory("saePU");
		entityManager = entityFactory.createEntityManager();
	}

	public T find(Class<T> entityClass, int id) {
		T result = (T) entityManager.find(entityClass, id);
		return result;
	}

	public List<T> find(String query) {
		return entityManager.createQuery(query).getResultList();
	}

	public T getReference(Class<T> entityClass, Long id) {
		return (T) entityManager.getReference(entityClass, id);
	}

	public static <K> K findNativeQuery(String query) {
		return (K) entityManager.createNativeQuery(query).getSingleResult();
	}

	public static <K> List<K> findNativeQueryList(String query) {
		return (List<K>) entityManager.createNativeQuery(query).getResultList();
	}

	public static Query findQueryParameterized(String query) {
		return entityManager.createQuery(query);
	}

	public static Query findNativeQueryParameterized(String query) {
		return entityManager.createNativeQuery(query);
	}

	public static Query createNamedQuery(String namedQuery) {
		return entityManager.createNamedQuery(namedQuery);
	}

	public static Query createQuery(String query) {
		return entityManager.createQuery(query);
	}

	public static void persistAluno(Aluno aluno) {
		entityManager.getTransaction().begin();
		entityManager.persist(aluno);
		entityManager.getTransaction().commit();
	}

	public static void updateAluno(Aluno aluno) {
		entityManager.getTransaction().begin();
		entityManager.merge(aluno);
		entityManager.getTransaction().commit();
	}

	public static void removerAluno(Aluno aluno) {
		entityManager.getTransaction().begin();
		entityManager.remove(aluno);
		entityManager.getTransaction().commit();
	}

	public static void persistAdministrador(Administrador administrador) {
		entityManager.getTransaction().begin();
		entityManager.persist(administrador);
		entityManager.getTransaction().commit();
	}

	public static void updateAdministrador(Administrador administrador) {
		entityManager.getTransaction().begin();
		entityManager.merge(administrador);
		entityManager.getTransaction().commit();
	}

	public static void removerAdministrador(Administrador administrador) {
		entityManager.getTransaction().begin();
		entityManager.remove(administrador);
		entityManager.getTransaction().commit();
	}

	public static void persistDisciplina(Disciplina disciplina) {
		entityManager.getTransaction().begin();
		entityManager.persist(disciplina);
		entityManager.getTransaction().commit();
	}

	public static void updateDisciplina(Disciplina disciplina) {
		entityManager.getTransaction().begin();
		entityManager.merge(disciplina);
		entityManager.getTransaction().commit();
	}

	public static void removerDisciplina(Disciplina disciplina) {
		entityManager.getTransaction().begin();
		entityManager.remove(disciplina);
		entityManager.getTransaction().commit();
	}

	public static void persistProfessor(Professor professor) {
		entityManager.getTransaction().begin();
		entityManager.persist(professor);
		entityManager.getTransaction().commit();
	}

	public static void updateProfessor(Professor professor) {
		entityManager.getTransaction().begin();
		entityManager.merge(professor);
		entityManager.getTransaction().commit();
	}

	public static void removerProfessor(Professor professor) {
		entityManager.getTransaction().begin();
		entityManager.remove(professor);
		entityManager.getTransaction().commit();
	}

	public static void persistTurma(Turma turma) {
		entityManager.getTransaction().begin();
		entityManager.persist(turma);
		entityManager.getTransaction().commit();
	}

	public static void updateTurma(Turma turma) {
		entityManager.getTransaction().begin();
		entityManager.merge(turma);
		entityManager.getTransaction().commit();
	}

	public static void removerTurma(Turma turma) {
		entityManager.getTransaction().begin();
		entityManager.remove(turma);
		entityManager.getTransaction().commit();
	}
	
	public static void persistDisciplinaProfessor(DisciplinaProfessor disciplinaProfessor) {
		entityManager.getTransaction().begin();
		entityManager.persist(disciplinaProfessor);
		entityManager.getTransaction().commit();
	}

	public static void updateDisciplinaProfessor(DisciplinaProfessor disciplinaProfessor) {
		entityManager.getTransaction().begin();
		entityManager.merge(disciplinaProfessor);
		entityManager.getTransaction().commit();
	}

	public static void removerDisciplinaProfessor(DisciplinaProfessor disciplinaProfessor) {
		entityManager.getTransaction().begin();
		entityManager.remove(disciplinaProfessor);
		entityManager.getTransaction().commit();
	}

	public static void persistNoticia(Noticia noticia) {
		entityManager.getTransaction().begin();
		entityManager.persist(noticia);
		entityManager.getTransaction().commit();
	}

	public static void updateNoticia(Noticia noticia) {
		entityManager.getTransaction().begin();
		entityManager.merge(noticia);
		entityManager.getTransaction().commit();
	}

	public static void removerNoticia(Noticia noticia) {
		entityManager.getTransaction().begin();
		entityManager.remove(noticia);
		entityManager.getTransaction().commit();
	}
	
	public static void persistMatricula(Matricula matricula) {
		entityManager.getTransaction().begin();
		entityManager.persist(matricula);
		entityManager.getTransaction().commit();
	}

	public static void updateMatricula(Matricula matricula) {
		entityManager.getTransaction().begin();
		entityManager.merge(matricula);
		entityManager.getTransaction().commit();
	}

	public static void removerMatricula(Matricula matricula) {
		entityManager.getTransaction().begin();
		entityManager.remove(matricula);
		entityManager.getTransaction().commit();
	}
	
	public static void persistUsuario(Usuario usuario) {
		entityManager.getTransaction().begin();
		entityManager.persist(usuario);
		entityManager.getTransaction().commit();
	}

	public static void updateUsuario(Usuario usuario) {
		entityManager.getTransaction().begin();
		entityManager.merge(usuario);
		entityManager.getTransaction().commit();
	}

	public static void removerUsuario(Usuario usuario) {
		entityManager.getTransaction().begin();
		entityManager.remove(usuario);
		entityManager.getTransaction().commit();
	}


}
